#ifndef ETHERCAT_CONFIG_H
#define ETHERCAT_CONFIG_H

#include <string>
#include <fstream>
#include <iostream>
#include "EtherCATMaster.h"

// EtherCAT配置类，用于从配置文件中读取配置信息
class EtherCATConfig {
private:
    std::string configFile;
    MasterType masterType;
    std::string interfaceName;
    
public:
    EtherCATConfig(const std::string& configFile = "ethercat.conf")
        : configFile(configFile), masterType(MasterType::SOEM), interfaceName("eth0") {
    }
    
    // 从配置文件加载配置
    bool loadConfig() {
        std::ifstream file(configFile);
        if (!file.is_open()) {
            std::cerr << "Warning: Could not open config file " << configFile << ", using default settings." << std::endl;
            return false;
        }
        
        std::string line;
        while (std::getline(file, line)) {
            // 跳过注释和空行
            if (line.empty() || line[0] == '#') {
                continue;
            }
            
            size_t pos = line.find('=');
            if (pos != std::string::npos) {
                std::string key = line.substr(0, pos);
                std::string value = line.substr(pos + 1);
                
                // 去除前后空格
                key.erase(0, key.find_first_not_of(" \t"));
                key.erase(key.find_last_not_of(" \t") + 1);
                value.erase(0, value.find_first_not_of(" \t"));
                value.erase(value.find_last_not_of(" \t") + 1);
                
                if (key == "master_type") {
                    if (value == "igh") {
                        masterType = MasterType::IGH;
                    } else if (value == "soem") {
                        masterType = MasterType::SOEM;
                    } else {
                        std::cerr << "Warning: Unknown master type '" << value << "', using default." << std::endl;
                    }
                } else if (key == "interface") {
                    interfaceName = value;
                }
            }
        }
        
        file.close();
        return true;
    }
    
    // 保存配置到文件
    bool saveConfig() {
        std::ofstream file(configFile);
        if (!file.is_open()) {
            std::cerr << "Error: Could not open config file " << configFile << " for writing." << std::endl;
            return false;
        }
        
        file << "# EtherCAT Master Configuration\n";
        file << "# master_type: igh or soem\n";
        file << "master_type=" << (masterType == MasterType::IGH ? "igh" : "soem") << "\n";
        file << "# interface: network interface name\n";
        file << "interface=" << interfaceName << "\n";
        
        file.close();
        return true;
    }
    
    // 获取主站类型
    MasterType getMasterType() const {
        return masterType;
    }
    
    // 设置主站类型
    void setMasterType(MasterType type) {
        masterType = type;
    }
    
    // 获取网络接口名称
    std::string getInterfaceName() const {
        return interfaceName;
    }
    
    // 设置网络接口名称
    void setInterfaceName(const std::string& name) {
        interfaceName = name;
    }
};

#endif // ETHERCAT_CONFIG_H